package com.globalwave.pos.purchase.service;

import java.util.ArrayList;
import java.util.List;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;

import com.globalwave.customer.entity.CustomerProfile;
import com.globalwave.customer.service.CustomerProfileBO;
import com.globalwave.pos.inventory.entity.ProductItem;
import com.globalwave.pos.payment.entity.PaymentLine;
import com.globalwave.pos.purchase.entity.PurchasesOrder;
import com.globalwave.test.BaseTestCase;

public class PurchasesOrderBOTest extends BaseTestCase {

	@Before
	public void init() throws Exception {
		initUser01() ;
	}
	
	@Test
	public void addItem() throws Exception {
		execute("com/globalwave/pos/inventory/sql/ItemMasterPriceBOTest.sql") ;
		execute("com/globalwave/pos/inventory/sql/ItemMasterPriceBOTest_validateSalable_20110905.sql") ;
		execute("com/globalwave/pos/customer/service/sql/customer_1.sql") ;
		
		
		ProductItem item = new ProductItem();
		item.setItem_number(12345L) ;
		
		PurchasesOrderBO bo = getPurchasesOrderBO() ;
		bo.addItem(null, item, 2) ;
		
		PurchasesOrder order = bo.getOrder() ;
		
		CustomerProfile cp = getCustomerProfileBO().get(1L) ;
		order.setSubscriber_number(cp.getCid()) ;
		order.setSubscriber_type(cp.getType_()) ;

		Assert.assertEquals(new Double(60), order.getOriginal_price()) ;
		
		Assert.assertEquals(1, order.getLines().size()) ;
		

		bo.addItem(order, item, 1) ;
		


		Assert.assertEquals(new Double(90), order.getOriginal_price()) ;
		
		Assert.assertEquals(1, order.getLines().size()) ;
		
		item = new ProductItem();
		item.setItem_number(23456L) ;
		
		bo.addItem(order, item, 2) ;
		

		Assert.assertEquals(new Double(170), order.getOriginal_price()) ;
		
		Assert.assertEquals(2, order.getLines().size()) ;
		
		this.jdbcDao.getConnection().setAutoCommit(false) ;
		
		bo.comfirm(order) ;
		
		this.jdbcDao.getConnection().commit();
		
		
		

		
		this.jdbcDao.getConnection().setAutoCommit(false) ;
		
		bo.autoShip() ;
		
		this.jdbcDao.getConnection().commit();
		
		List<PaymentLine> lines = new ArrayList<PaymentLine>() ;
		order.setPaymentLines(lines) ;
		
		PaymentLine line = new PaymentLine() ;
		lines.add(line) ;

		line.setPayment_method("C") ;
		line.setPayment_amount(new Double(100)) ;
		line.setCurrency_code("CNY") ;
		line.setPayment_date(order.getSale_date()) ;
	
		line = new PaymentLine() ;
		lines.add(line) ;

		line.setPayment_method("B") ;
		line.setPayment_amount(new Double(70)) ;
		line.setCurrency_code("CNY") ;
		line.setPayment_date(order.getSale_date()) ;
		
		
		this.jdbcDao.getConnection().setAutoCommit(false) ;
		
		bo.pay(order) ;
		
		this.jdbcDao.getConnection().commit();
		
	}
	

	
	private PurchasesOrderBO getPurchasesOrderBO() throws Exception {
		PurchasesOrderBO bo = new PurchasesOrderBO() ;
		bo.setJdbcDao(jdbcDao) ;
		
		return bo ;
	}
	
	private CustomerProfileBO getCustomerProfileBO() throws Exception {
		CustomerProfileBO bo = new CustomerProfileBO() ;
		bo.setJdbcDao(jdbcDao) ;
		
		return bo ;
	}
}
